<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="authStatus">
        <el-select
          v-model="queryParams.authStatus"
          placeholder="选择审核状态状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="authList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户id" align="center" prop="userId"/>
      <el-table-column label="身份证号" align="center" prop="idCard"/>
      <el-table-column label="真实姓名" align="center" prop="realName"/>
      <el-table-column label="正面" align="center" prop="obverseImg">
        <template slot-scope="scope">
          <el-image
            style="width: 306px; height: 228px"
            :src="requestUrl + scope.row.obverseImg"
            :preview-src-list="[(requestUrl + scope.row.obverseImg)]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="反面" align="center" prop="reverseImg">
        <template slot-scope="scope">
          <el-image
            style="width: 306px; height: 228px"
            :src="requestUrl + scope.row.reverseImg"
            :preview-src-list="[(requestUrl + scope.row.reverseImg)]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="authStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.authStatus == 0">待审核</span>
          <span v-if="scope.row.authStatus == 1">审核通过</span>
          <span v-if="scope.row.authStatus == -1">审核拒绝</span>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createTime"/>
      <el-table-column label="审核时间" align="center" prop="authTime"/>
      <el-table-column label="最后更新时间" align="center" prop="lastModifiedTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            v-if="scope.row.authStatus == 0"
            @click="handleAuth(scope.row,1)"
          >审核通过
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            v-if="scope.row.authStatus == 0"
            @click="handleAuth(scope.row,-1)"
          >审核拒绝
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 用户明细 -->
    <el-dialog
      :title="title"
      :visible.sync="detailFormDisable"
      width="500px"
      append-to-body
    >
      <el-form ref="detailForm" :model="detailForm" label-width="120px">
        <el-form-item label="真实姓名">{{ detailForm.realName }}</el-form-item>
        <el-form-item label="身份证">{{ detailForm.idCard }}</el-form-item>
        <br>
        <el-form-item label="身份证正面: ">
          <el-image
            :src="requestUrl + detailForm.obverseImg"
            style="width: 306px; height: 228px"
            :preview-src-list="obverseImgList"/>
        </el-form-item>
        <br>
        <br>
        <el-form-item label="身份证反面: ">
          <el-image
            :src="requestUrl + detailForm.reverseImg"
            style="width: 306px; height: 228px"
            :preview-src-list="reverseImgList"/>
        </el-form-item>
        <br>
        <el-form-item label="申请时间">{{ detailForm.createTime }}</el-form-item>
        <el-form-item label="审核时间">{{ detailForm.authTime }}</el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import {listUserIdentifyAuth, userIdentifyAuth} from '@/api/admin/user';

export default {
  name: "Auth",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户实名认证表格数据
      authList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        authStatus: null,
        userId: null
      },
      // 表单参数
      form: {},
      statusOptions: [
        {
          value: 0,
          label: "待审核",
        },
        {
          value: 1,
          label: "审核通过",
        },
        {
          value: -1,
          label: "审核拒绝",
        },
      ],
      detailFormDisable: false,
      detailForm: {},
      requestUrl: this.$img.requestUrl,
      obverseImgList:[],
      reverseImgList:[],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户实名认证列表 */
    getList() {
      this.loading = true;
      listUserIdentifyAuth(this.queryParams).then(response => {
        this.authList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.authId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户实名认证";
    },
    /** 删除按钮操作 */
    handleAuth(row, authStatus) {
      let authForm = {authId: row.authId, authStatus: authStatus}
      let title = '';
      if (authStatus == 1) {
        title = "通过";
      } else if (authStatus == -1) {
        title = "拒绝";
      }
      this.$confirm('是否确认' + title + '该审核?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return userIdentifyAuth(authForm);
      }).then(() => {
        this.getList();
        this.msgSuccess("审核成功");
      }).catch(function () {
      });
    },
    // 表单重置
    resetDetailForm() {
      this.detailForm = {};
      this.resetForm("detailForm");
    },
    handleView(row) {
      this.resetDetailForm();
      this.title = '查看详情'
      this.detailForm = {...row};
      this.obverseImgList.push(this.requestUrl + row.obverseImg)
      this.reverseImgList.push(this.requestUrl + row.reverseImg)
      this.detailFormDisable = true;
    }
  }
};
</script>
